package com.framework.test.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Random;

public class DataUtil {
    private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String CHARSET = "UTF-8";
    private static final int KEY_SIZE = 128;
    private static final String MD5_ALGORITHM = "MD5";

    // AES加密
    public static String aesEncrypt(String data, String key, String iv) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(Base64.getDecoder().decode(iv));
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(CHARSET));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    // AES解密
    public static String aesDecrypt(String encryptedData, String key, String iv) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(Base64.getDecoder().decode(iv));
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedBytes, CHARSET);
    }

    // 生成AES密钥
    public static String generateAesKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(KEY_SIZE); // 设置密钥长度
        SecretKey secretKey = keyGenerator.generateKey();
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    // 生成AES IV
    public static String generateAesIv() {
        byte[] iv = new byte[16];
        new SecureRandom().nextBytes(iv);
        return Base64.getEncoder().encodeToString(iv);
    }

    // MD5加密
    public static String md5(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance(MD5_ALGORITHM);
        byte[] digestedBytes = md.digest(data.getBytes(CHARSET));
        StringBuilder sb = new StringBuilder();
        for (byte b : digestedBytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    // 生成随机字符串
    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(characters.charAt(random.nextInt(characters.length())));
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
        String key = generateAesKey();
        String iv = generateAesIv();

        String data = "Hello, world!";
        String encrypted = aesEncrypt(data, key, iv);
        String decrypted = aesDecrypt(encrypted, key, iv);

        System.out.println("Original data: " + data);
        System.out.println("Encrypted data: " + encrypted);
        System.out.println("Decrypted data: " + decrypted);

        String randomString = generateRandomString(10);
        System.out.println("Random string: " + randomString);

        String md5Hash = md5(data);
        System.out.println("MD5 hash: " + md5Hash);
    }
}